package DB;

import Model.*;

import java.sql.*;
import java.util.ArrayList;

public class DBRabat 
{
	private Connection con;
	
	private static DBRabat   instance = null;
    
    public static DBRabat getInstance()
    {
        if (instance == null)
        {
          instance = new DBRabat();
        }
        return instance;
    }
	
	public DBRabat()
	{
		con = DBConnection.getInstance().getDBcon();
	}
	
	public ArrayList<Rabat> getAlleRabater()
	{
		return miscWhere("");
	}
	
	private ArrayList<Rabat> miscWhere(String wClause)
	{
		ResultSet results;
		ArrayList<Rabat> list = new ArrayList<Rabat>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{ // read the customer from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);		 		

			while( results.next() )
			{
		     	 Rabat rabat = new Rabat();
		     	 rabat = buildRabat(results);	
                 list.add(rabat);	
			}
			stmt.close();
		}
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Rabat";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	private Rabat buildRabat(ResultSet results)
    {   
		DBRabat dbPost = new DBRabat();
		Rabat rabat = new Rabat();
          try
          {
        	  	rabat.setRabatid(results.getInt(1));
                rabat.setRabat((results.getInt(2)));
                rabat.setRabattype(results.getString(3));
          }
         catch(Exception e)
         {
             System.out.println("error in building the Rabat object");
         }
         return rabat;
    }
	
	private void create(String wClause)
	{
	    String query="INSERT INTO Rabat ";
	   
		if (wClause.length()>0)
		{
			query=query+ wClause;
			
			Statement stmt;
			String sql;     
			int rows;
			
			try
			{
			stmt = con.createStatement(); 
			rows = stmt.executeUpdate(query); 
			con.commit(); 
			stmt.close();
			}
			catch (Exception e)
			{ 
			  e.printStackTrace(); 
			}
		}
		else
		{

		}
	}
	
	public void createRabat(String type, int rabat)
	{
			String wClause = "VALUES " 
            + "('" + type   + "',"
            + "'" + rabat + "',)";

            create(wClause);
	}
	
	public void updateRabat(Rabat rabat)
	{
		
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		   ("UPDATE Rabat SET " 
				+ "Type = '" + rabat.getRabattype() + "'"
		   		+ "Rabat = '" + rabat.getRabat() + "'"
		   		+ " WHERE RabatId = " + rabat.getRabatid() + "");
		  prepstmt.executeUpdate();
		             
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	}
	
	private Rabat singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Rabat rabat = new Rabat();
	    String query =  buildQuery(wClause);
		try{ // read the zipCity from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
	 				rabat = buildRabat(results);
                            //association is to be build
                            stmt.close();	                            
			}
		
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return rabat;
	}
	
	public void deleteRabat(String navn)
	{
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		    ("DELETE FROM Rabat WHERE rabattype = " + navn + "");   
		  prepstmt.executeUpdate();          
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	} 
	public Rabat findRabat(int rabat) 
    {
    	String wClause = "  rabatid = " + Integer.toString(rabat) + "";
    	return singleWhere(wClause, true);
    }
	public Rabat findRabatNavn(String navn)
	{
		String wClause = " rabattype LIKE '%" + navn + "%'";
    	return singleWhere(wClause, true);
	}
}
